<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/22 0022
 * Time: 13:53
 */

namespace app\admin\controller;

use think\Request;

class Role extends Base
{
    protected $_roleModel;

    public function initialize(){
        $this -> _initialize();
        $this->_roleModel = new \app\model\model\Role();

    }

    public function lists(){
        return $this->fetch();
    }

    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['username'] = ['like',"%$name%"];
        }
        $data = $this->_roleModel->getList($where, input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }

    public function add(){
        if(Request::instance()->post()) {
            $name = input('name');
            $power = $_POST['power'];
            if(empty($name)){
                return json_echo_msg(100,'请输入角色名称');
            }
            if(empty($power)){
                return json_echo_msg(101,'请选择角色权限');
            }
            $power = implode(',', $power);
            $res = $this->_roleModel->addOne([
                'name'              => $name,
                'power'             => $power,
                'create_time'       => time()
            ]);
            if($res === false){
                return json_echo_msg(101,'角色添加失败');
            }
            return json_echo_msg(200,'角色添加成功');
        } else {
            $menu_model = new \app\model\model\Authmenu();
            $field = 'id,parent_id,name,url,sort,type';
            $menu_list = $menu_model->order(['type'=>'asc','sort'=>'asc'])->column($field);
            if(empty($menu_list)){
                return false;
            }
            $menu_list_arr = [
                1=>['name'=>'基础管理', 'menu'=>[]],
                2=>['name'=>'询盘管理', 'menu'=>[]],
            //    3=>['name'=>'会员管理', 'menu'=>[]],
                4=>['name'=>'系统管理', 'menu'=>[]],
            ];
            foreach ($menu_list as $val){
                if(empty($val['parent_id'])){
                    $menu_list_arr[$val['type']]['menu'][$val['id']] = $val;
                } else {
                    if(isset($menu_list_arr[$val['type']]['menu'][$val['parent_id']])){
                        $menu_list_arr[$val['type']]['menu'][$val['parent_id']]['sub'][$val['id']] = $val;
                    }
                }
            }

            $this->assign('menu_list_arr', $menu_list_arr);
            return $this->fetch();
        }
    }

    public function up(){
        if($this -> request -> method() =='POST') {
            $name = input('name');
            $role_id = input('id');
            $power = $_POST['power'];
            if(empty($role_id)){
                return json_echo_msg(100,'缺少参数');
            }
            if(empty($name)){
                return json_echo_msg(101,'请输入角色名称');
            }
            if(empty($power)){
                return json_echo_msg(102,'请选择角色权限');
            }
            $role_info = $this->_roleModel->getOneById($role_id);
            if(empty($role_info)){
                return json_echo_msg(103,'没有数据');
            }
            $power = implode(',', $power);
            $res = $this->_roleModel->updateOne($role_id, [
                'name'              => $name,
                'power'             => $power
            ]);
            if($res === false){
                return json_echo_msg(104,'角色添加失败');
            }
            return json_echo_msg(200,'角色更新成功');
        } else {
            $role_id = input('id');
            $role_info = $this->_roleModel->getOneById($role_id);
            if(empty($role_info)){
                $this->error('没有数据');
            }
            $menu_ids = explode(',', $role_info->power);

            $menu_model = new \app\model\model\Authmenu();
            $field = 'id,parent_id,name,url,sort,type';
            $menu_list = $menu_model->order(['type'=>'asc','sort'=>'asc'])->column($field);
            if(empty($menu_list)){
                return false;
            }
            $menu_list_arr = [
                1=>['name'=>'基础管理', 'menu'=>[]],
                2=>['name'=>'询盘管理', 'menu'=>[]],
                //    3=>['name'=>'会员管理', 'menu'=>[]],
                4=>['name'=>'系统管理', 'menu'=>[]],
            ];
            foreach ($menu_list as $val){
                $val['is_checked'] = false;
                if(in_array($val['id'], $menu_ids)){
                    $val['is_checked'] = true;
                }
                if(empty($val['parent_id'])){
                    $menu_list_arr[$val['type']]['menu'][$val['id']] = $val;
                } else {
                    if(isset($menu_list_arr[$val['type']]['menu'][$val['parent_id']])){
                        $menu_list_arr[$val['type']]['menu'][$val['parent_id']]['sub'][$val['id']] = $val;
                    }
                }
            }
            $this->assign('data', $role_info);
            $this->assign('menu_list_arr', $menu_list_arr);
            return $this->fetch();
        }
    }

    public function del(){
        $res = $this->_roleModel->del(input('id'));
        if($res == true){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

    /**
     * 批量删除
     * */
    public function delAll(){
        $re = $this->_roleModel->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(500,'删除失败！');
        }
    }

}